package com.beidouapp.service.impl;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.beidouapp.domain.example.RoleAndResourceExample;
import com.beidouapp.domain.example.RoleAndResourceExample.Criteria;
import com.beidouapp.domain.pojo.Resource;
import com.beidouapp.domain.pojo.RoleAndResource;
import com.beidouapp.dao.ResourceDao;
import com.beidouapp.dao.RoleAndResourceManager;
import com.beidouapp.service.ResourceService;

import org.springframework.util.CollectionUtils;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceDao resourceDao;


    @Override
    public List<Resource> findAllResource() {
        return resourceDao.findResourcesByResource(new Resource());
    }

    @Override
    public List<Resource> findRoleResourcesByRoleId(Long roleId) {
        return resourceDao.findRoleResourcesByRoleId(roleId);
    }

    @Override
    public int removeRoleAndResourceByRoleId(Long roleId) {
        return resourceDao.removeRoleAndResourceByRoleId(roleId);
    }

    @Override
    public int addRoleAndResourceBatch(List<RoleAndResource> list) {
        return resourceDao.addRoleAndResourceBatch(list);
    }

    @Override
    public int removeRoleAndResourceBatch(Map<String, Object> params) {
        return resourceDao.removeRoleAndResourceBatch(params);
    }

    //------------------------

    @Autowired
    private RoleAndResourceManager roleAndResourceManager;


    @Override
    public void grantResources(Long roleId, Set<Long> addIdList, Set<Long> delIdList) {

        if (!CollectionUtils.isEmpty(delIdList)) {
            List<Long> tempDelList = new ArrayList<Long>(delIdList.size());
            for (Long delId : delIdList) {
                tempDelList.add(delId);
            }
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("roleId", roleId);
            params.put("list", tempDelList);
            this.resourceDao.removeRoleAndResourceBatch(params);
        }

        if (!CollectionUtils.isEmpty(addIdList)) {
            List<RoleAndResource> rarList = new ArrayList<RoleAndResource>(addIdList.size());
            for (Long addId : addIdList) {
                RoleAndResource rgr = new RoleAndResource();
                rgr.setResourceId(addId);
                rgr.setRoleId(roleId);
                rarList.add(rgr);
            }
            this.addRoleAndResourceBatch(rarList);
        }
    }

    @Override
    public List<Resource> findResourceByAppAndRole(String appShowName, String roleCode) {
        List<Resource> list = new ArrayList<Resource>();
        Resource rc = new Resource();
        rc.setCode("framework_dict");
        list.add(rc);

        rc = new Resource();
        rc.setCode("user_manager");
        list.add(rc);
        return list;
    }

    @Override
    public List<Resource> findResourceByApp(String appShortName) {
        List<Resource> list = new ArrayList<Resource>();


        return list;
    }

	@Override
	public List<Resource> findRoleResourcesByContactsLoginName(String LoginName) {
		return resourceDao.findRoleResourcesByContactsLoginName(LoginName);
	}


}
